<?php
namespace app\admin\logic;

use app\common\logic\Base;
use think\facade\App;

class UserCenter extends Base {
    
    /**
     * @desc 用户列表
     * @param array $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($params) {
        $where = [];

        if($params['userName']) {
            $where[] = ['user_name', '=', $params['userName']];
        }

        if($params['phone']) {
            $where[] = ['user_phone', '=', $params['phone']];
        }
    
        if($params['email']) {
            $where[] = ['user_email', '=', $params['email']];
        }
        
        $list = App::model('User')
            ->where($where)
            ->order('user_id desc')
            ->paginate($params['pageSize'], false, ['query' => request()->param()]);
    
        $pagination = $list->render();
        if($list) {
            $list = $list->toArray();
            $list = $list['data'];
        }
        return [
            'list'         => $list,
            'params'       => $params,
            'pagination'   => $pagination
        ];
    }
    
    /**
     * @desc 编辑配置
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit($params) {
        $info = App::model('User')
            ->where(['user_id' => $params['id']])
            ->find();

        return [
            'info'    => $info,
            'params'  => $params
        ];
    }
    
    /**
     * @desc 栏目分类 (添加、编辑)
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function operateAjax($params) {
        $result = App::model('User')
            ->where(['user_id' => $params['id']])
            ->update([
                'user_level' => $params['userLevel']
            ]);
        
        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }
}